import Layout from '@/layout'
import PageContent from '@/layout/pageContent.vue'
import {LOGICAL, MENU_TYPE} from "@/constantData";
import {isArr} from "@/utils/arrayUtil";
import {authentication} from "@/jurisdiction/authentication";

let businessRouter = [

  {
    path: 'bus',
    redirect: '/chit',
    component: Layout,
    children: [
      // 保单管理
      {
        path: '/chit',
        component: PageContent,
        redirect: '/chit/chitList',
        name: 'chit',
        // authority: {role: ['business:menu:aqjc'], logical: LOGICAL.OR},
        hidden: false,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '保单管理'},
        children: [
          // 保单列表
          {
            path: 'chitList',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/chit',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/chitListPage'),
            name: 'chitList',
            meta: {title: '保单列表'}
          },

          // 保单列表导入
          {
            path: 'importChitListData',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/importData/importListData.vue'),
            name: 'importListData',
            meta: {title: '保单列表导入'}
          },
          // 保单明细导入
          {
            path: 'importChitLDetailData',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/importData/importDetailData.vue'),
            name: 'importDetailData',
            meta: {title: '保单明细导入'}
          },
          // 理赔数据导入
          {
            path: 'importClaimSettlementData',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/importData/importClaimSettlementData.vue'),
            name: 'importClaimSettlementData',
            meta: {title: '理赔数据导入'}
          },
          // 抽查数据导入
          {
            path: 'importSpotCheckData',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/importData/importSpotCheckData.vue'),
            name: 'importSpotCheckData',
            meta: {title: '抽查数据导入'}
          },
          // 保单新增/修改
          {
            path: 'chitAdd',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/addPage'),
            name: 'chitAdd',
            meta: {title: '保单入库'}
          },
          //   保单查看
          {
            path: 'chitInfoLook',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chitLook'),
            name: 'chitLook',
            meta: {title: '保单查看'}
          },
          // 保单详情
          {
            path: 'chitInfo',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList/chitInfoLook',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/infoPage'),
            name: 'chitInfo',
            meta: {title: '保单详情'}
          },
          // 保单详情 -- 修改
          {
            path: 'chitInfoAdd',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/chit/chitList/chitInfoLook/chitInfo',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/addPage'),
            name: 'chitAdd',
            meta: {title: '保单入库'}
          },
          //   保单查看
          {
            path: 'chitLook',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/chit',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chitLook'),
            props: { source: 'chitLook' },
            name: 'chitLook1',
            meta: {title: '保单查看'}
          },
        ],
      },
      // 地图相关
      {
        path: '/thematicMap',
        component: PageContent,
        redirect: '/thematicMap/baseMap',
        name: 'defectType',
        // authority: {role: ['business:menu:xtgl'], logical: LOGICAL.OR},
        hidden: false,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '地图相关'},
        children:[
          //   专题图
          {
            path: 'baseMap',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/thematicMap',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chitLook'),
            props: { source: 'baseMap' },
            name: 'baseMap',
            meta: {title: '专题图'}
          },
          //   保险一张图
          {
            path: 'bxView',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/baseMap',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/bxView'),
            props: { source: 'bxView' },
            name: 'zsView',
            meta: {title: '保险一张图'}
          },
          //   保险一张图
          {
            path: 'szView',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/baseMap',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/szView'),
            name: 'zsView',
            meta: {title: '灾害分析专题图'}
          },
          //   长势专题图
          {
            path: 'zsView',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/baseMap',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/zsView'),
            name: 'zsView',
            meta: {title: '长势专题图'}
          },
          //   灾害预警
          {
            path: 'forewarning',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/baseMap',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/forewarning'),
            name: 'zsView',
            meta: {title: '灾害预警'}
          },
        ]
      },
      // 系统管理
      {
        path: '/system',
        component: PageContent,
        redirect: '/system/sysSupervisionDeptList',
        name: 'defectType',
        // authority: {role: ['business:menu:xtgl'], logical: LOGICAL.OR},
        hidden: false,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '系统管理'},
        children: [
          // 导入任务
          {
            path: 'importTaskListPage',
            keep: false,
            // authority: {role: ['business:menu:jgbm'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/importTask/listPage.vue'),
            name: 'importTaskListPage',
            meta: {title: '导入任务'}
          },
          // 导入任务明细
          {
            path: 'importTaskInfoPage',
            keep: false,
            // authority: {role: ['business:menu:jgbm'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/system/importTaskListPage',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/importTask/infoPage.vue'),
            name: 'importTaskInfoPage',
            meta: {title: '导入任务明细'}
          },

          // 监管地域管理
          // {
          //   path: 'sysSupervisionDeptList',
          //   keep: true,
          //   authority: {role: ['business:menu:jgbm'], logical: LOGICAL.OR},
          //   hidden: false,
          //   parentPath: '/system',
          //   menuType: MENU_TYPE.MENU,
          //   component: () => import('@/views/system/sysSupervisionDept/listPage.vue'),
          //   name: 'sysSupervisionDept_list',
          //   meta: {title: '监管地域管理'}
          // },

          // 企业管理
          // {
          //   path: 'sysEnterpriseList',
          //   keep: true,
          //   authority: {role: ['business:menu:qygl'], logical: LOGICAL.OR},
          //   hidden: false,
          //   parentPath: '/system',
          //   menuType: MENU_TYPE.MENU,
          //   component: () => import('@/views/system/sysEnterprise/listPage.vue'),
          //   name: 'sysEnterprise_list',
          //   meta: {title: '企业管理'}
          // },
          // 地域管理
          {
            path: 'dept',
            keep: true,
            authority: {role: ['system:user:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/dept'),
            name: 'dept',
            meta: {title: '地域管理'}
          },

          // 用户管理
          {
            path: 'sysUserList',
            keep: true,
            authority: {role: ['system:user:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/sysUser/listPage.vue'),
            name: 'sysUser_list',
            meta: {title: '用户管理'}
          },

          // 字典管理
          {
            path: 'dictList',
            keep: true,
            authority: {role: ['system:dict:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/dict/index.vue'),
            name: 'dict_list',
            meta: {title: '字典管理'}
          },
          // 险种管理
          {
            path: 'typesOfInsurance',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/typesOfInsurance'),
            name: 'typesOfInsurance',
            meta: {title: '险种管理'}
          },
          // 通知公告
          {
            path: 'noticeAnnouncement',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/noticeAnnouncement'),
            name: 'noticeAnnouncement',
            meta: {title: '通知公告'}
          },
          //   保单查看
          {
            path: 'chitInformationLook',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chitLook'),
            name: 'chitInformationLook',
            meta: {title: '保单查看'}
          },
          // 保单详情
          {
            path: 'chitInformation',
            keep: false,
            // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/chit/infoPage'),
            name: 'chitInformation',
            meta: {title: '保单详情'}
          },
          //农户档案（旧）
          {
            path: 'farmersFile',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/farmersFile'),
            name: 'farmersFile',
            meta: {title: '农户档案(旧)'}
          },
            //农户档案（新）
          {
            path: 'farmersInfo',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/farmersInfo'),
            name: 'farmersInfo',
            meta: {title: '农户档案'}
          },
            //精准赔偿
          {
            path: 'precisioncompensate',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/PrecisionCompensate'),
            name: 'precisioncompensate',
            meta: {title: '精准理赔'}
          },
          //现场验标
          {
            path: 'localeinspection',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/LocaleInspection'),
            name: 'localeinspection',
            meta: {title: '现场验标'}
          },
          //系统配置项
          {
            path: 'systemconfigoption',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/SystemConfigOption'),
            name: 'systemconfigoption',
            meta: {title: '系统配置项'}
          },
          //报案管理，报案信息查询页面
          {
            path: 'reportInformation',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/reportInformation'),
            name: 'reportInformation',
            meta: {title: '报案管理'}
          },
          // 险种生育期管理
          {
            path: 'childBearingPeriod',
            keep: false,
            authority: {role: ['system:childBearingPeriod:admin'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/system/typesOfInsurance',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/childBearingPeriod'),
            name: 'childBearingPeriod',
            meta: {title: '险种生育期管理'}
          },
          // 字典类别子类
          {
            path: 'dictInfo',
            keep: false,
            authority: {role: ['system:dict:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system/dictList',
            menuType: MENU_TYPE.INFO,
            component: () => import('@/views/system/dict/data.vue'),
            name: 'dict_info',
            meta: {title: '字典数据'},
            children: []
          },

          {
            path: 'management',
            keep: false,
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/baseDataManagement'),
            name: 'management',
            meta: { title: '基础数据管理' }
          },
          {
            path: 'statisticaltable',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/StatisticalTable'),
            name: 'statisticaltable',
            meta: { title: '统计报表' }
          },
          {
            path: 'warning',
            keep: false,
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/RegulatoryWarning'),
            name: 'warning',
            meta: { title: '监管预警' }
          },
          //  现场验标
          {
            path: 'bidChecking',
            keep: true,
            authority: {role: ['system:ordertype:admin'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/bidChecking'),
            name: 'bidChecking',
            meta: {title: '现场验标'}
          },
        ],
      },

      // 个人中心
      {
        path: '/mine',
        component: PageContent,
        redirect: '/mine/personalSetting',
        name: 'mine',
        hidden: true,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '个人中心'},
        children: [
          // 个人设置
          {
            path: 'personalSetting',
            keep: false,
            authority: {role: ['A:B:C'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/mine',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/mine'),
            name: 'personal_setting',
            meta: {title: '个人设置'}
          },
        ],
      },
    ]
  },

  {
    path: '',
    name: 'home',
    component: () => import('@/views/home')
  },

  {
    path: '/policy',
    name: 'policy',
    redirect: '/statistics',
    component: Layout,
    menuType: MENU_TYPE.MENU,
    children: [
      {
        path: 'statistics',
        keep: false,
        // authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
        hidden: true,
        parentPath: '/policy',
        menuType: MENU_TYPE.MENU,
        component: () => import('@/views/policyStatistics'),
        name: 'statistics',
        meta: {title: '保单明细导入'}
      },
    ]
  },
]

const changeBusinessRouter = () => {
  const root = _.cloneDeep(businessRouter[0]);

  // 移除不需要的节点
  root.children = removeInfoNodes(_.cloneDeep(root.children));

  // 进行深度优先搜索
  const stack = [root];
  while (stack.length > 0) {
    const node = stack.pop();

    node.hidden = node.hidden ? true : (node.authority != null ? !authentication(node.authority) : false)

    if (isArr(node.children)) {
      stack.push(...node.children);
    }
  }

  return [root];
};

const getKeepList = () => {
  return getPropertyArray(_.cloneDeep(businessRouter[0]))
}

const getPropertyArray = (node) => {

  let result = [];

  // 检查节点的属性是否存在
  if (node.keep && node.name != null && node.name != '') {
    result.push(node.name);
  }

  // 遍历子节点
  if (isArr(node.children)) {
    for (let i = 0; i < node.children.length; i++) {
      const child = node.children[i];
      result = result.concat(getPropertyArray(child));
    }
  }

  return result;
};

const removeInfoNodes = (routes) => {
  const newRoutes = [];

  routes.forEach(route => {
    if (route.menuType !== MENU_TYPE.INFO) {
      // 如果当前节点的 menuType 不是 MENU_TYPE.INFO，则将其添加到新路由配置中
      const newRoute = {...route};

      if (route.children) {
        // 递归处理子节点
        newRoute.children = removeInfoNodes(route.children);
      }

      newRoutes.push(newRoute);
    }
  });

  return newRoutes;
}

const getNavigationBaseList = () => {
  let NavigationBaseList = traverseTree(businessRouter, '')
  return NavigationBaseList.filter(item => item.title && item.title !== '').map(item => {
    item.parentPath = item.parentPath.replace('/bus','')
    item.path = item.path.replace('/bus','')
    return item
  })
}

const traverseTree = (tree, parentPath = '') => {
  let result = [];

  for (const node of tree) {
    const path = `${parentPath}/${node.path}`.replace(/\/+/g, '/');

    result.push({title: node?.meta?.title, parentPath: node.parentPath, path, menuType: node.menuType});

    if (node.children && node.children.length > 0) {
      const childNodes = traverseTree(node.children, path);
      result = [...result, ...childNodes];
    }
  }

  return result;
};

export {businessRouter, getNavigationBaseList, getKeepList, changeBusinessRouter}
